#include "InformationView.h"
#include "StyleLoader.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QGraphicsDropShadowEffect>
#include <QShowEvent>
#include <QDebug>

InformationView::InformationView(QWidget *parent) 
    : QWidget(parent)
    , isDragging(false)
    , m_nWidth(400)
    , m_nHeight(600)
{
    // 无边框窗口
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_DeleteOnClose);
    
    initData();
    initForm();
    initializeLayout();
    applyStyles();
}

InformationView::~InformationView()
{
    // Qt 会自动删除子对象，无需手动删除
}

void InformationView::setWindowSize(int width, int height)
{
    m_nWidth = width;
    m_nHeight = height;
    
    if (mainWidget) {
        mainWidget->setFixedSize(m_nWidth, m_nHeight);
    }
    
    // 调整窗口大小以容纳主控件和阴影
    resize(m_nWidth + 40, m_nHeight + 40);
}

void InformationView::initData()
{
    // 初始化数据
}

void InformationView::initForm()
{
    // 创建按钮
    infoButton = createIconButton(":/resource/svg/info.svg", "信息");
    disturbButton = createIconButton(":/resource/svg/disturb.svg", "打扰");
    restButton = createIconButton(":/resource/svg/rest.svg", "休息");
    washroomButton = createIconButton(":/resource/svg/washroom.svg", "洗手间");
    customButton = createIconButton(":/resource/svg/custom.svg", "自定义");
    
    // 创建自定义输入框
    customEdit = new NewLineEdit(this);
    customEdit->setButtonText("确定");
    customEdit->setLeftIcon(":/resource/svg/custom.svg");
    customEdit->setAlignment(Qt::AlignCenter);
    customEdit->setPlaceholderText("请输入自定义原因...");
    customEdit->setVisible(false);
    
    // 连接信号槽
    connect(infoButton, &QPushButton::clicked, this, &InformationView::onBtnInfoClicked);
    connect(disturbButton, &QPushButton::clicked, this, &InformationView::onBtnDisturbClicked);
    connect(restButton, &QPushButton::clicked, this, &InformationView::onBtnRestClicked);
    connect(washroomButton, &QPushButton::clicked, this, &InformationView::onBtnWashroomClicked);
    connect(customButton, &QPushButton::clicked, this, &InformationView::onBtnCustomClicked);
    connect(customEdit, &NewLineEdit::sigConfirm, this, &InformationView::onEditCustomConfirm);
}

void InformationView::initializeLayout()
{
    // 主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(0);
    
    // 主控件
    mainWidget = new QWidget(this);
    mainWidget->setObjectName("informationViewMain");
    mainWidget->setFixedSize(m_nWidth, m_nHeight);
    
    // 内容布局
    QVBoxLayout* contentLayout = new QVBoxLayout(mainWidget);
    contentLayout->setSpacing(15);
    contentLayout->setContentsMargins(20, 20, 20, 20);
    
    // 标题
    titleLabel = new QLabel("您为什么要暂停？", mainWidget);
    titleLabel->setObjectName("informationViewTitle");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setFixedHeight(60);
    contentLayout->addWidget(titleLabel);
    
    // 添加按钮
    contentLayout->addWidget(infoButton);
    contentLayout->addWidget(disturbButton);
    contentLayout->addWidget(restButton);
    contentLayout->addWidget(washroomButton);
    contentLayout->addWidget(customButton);
    
    // 添加自定义输入框
    contentLayout->addWidget(customEdit);
    
    // 添加弹性空间
    contentLayout->addStretch();
    
    mainWidget->setLayout(contentLayout);
    mainLayout->addWidget(mainWidget, 0, Qt::AlignCenter);
    setLayout(mainLayout);
    
    // 添加阴影效果
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(20);
    shadow->setOffset(0, 4);
    shadow->setColor(QColor(0, 0, 0, 60));
    mainWidget->setGraphicsEffect(shadow);
}

void InformationView::applyStyles()
{
    // 主控件样式
    mainWidget->setStyleSheet(R"(
        QWidget#informationViewMain {
            background-color: #FFFFFF;
            border-radius: 12px;
        }
    )");
    
    // 标题样式
    titleLabel->setStyleSheet(R"(
        QLabel#informationViewTitle {
            color: #2C2C2E;
            font-size: 18px;
            font-weight: bold;
            font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
        }
    )");
    
    // 按钮样式
    QString buttonStyle = R"(
        QPushButton {
            background-color: #F5F5F5;
            color: #2C2C2E;
            border: none;
            border-radius: 8px;
            padding: 15px 20px;
            font-size: 14px;
            font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
            text-align: left;
            min-height: 50px;
        }
        QPushButton:hover {
            background-color: #4CAF50;
            color: #FFFFFF;
        }
        QPushButton:pressed {
            background-color: #45a049;
        }
    )";
    
    infoButton->setStyleSheet(buttonStyle);
    disturbButton->setStyleSheet(buttonStyle);
    restButton->setStyleSheet(buttonStyle);
    washroomButton->setStyleSheet(buttonStyle);
    customButton->setStyleSheet(buttonStyle);
    
    // 自定义输入框样式
    customEdit->setStyleSheet(R"(
        NewLineEdit, QLineEdit {
            background-color: #F5F5F5;
            color: #2C2C2E;
            border: 2px solid #E0E0E0;
            border-radius: 8px;
            padding: 12px;
            font-size: 14px;
            font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
            min-height: 50px;
        }
        NewLineEdit:hover, QLineEdit:hover {
            border-color: #4CAF50;
        }
        NewLineEdit:focus, QLineEdit:focus {
            border-color: #4CAF50;
            background-color: #FFFFFF;
        }
    )");
}

QPushButton* InformationView::createIconButton(const QString& iconPath, const QString& text)
{
    QPushButton* button = new QPushButton(this);
    
    // 创建水平布局
    QHBoxLayout* layout = new QHBoxLayout(button);
    layout->setContentsMargins(15, 0, 15, 0);
    layout->setSpacing(15);
    
    // 创建图标标签
    QLabel* iconLabel = new QLabel(button);
    QPixmap pixmap(iconPath);
    if (!pixmap.isNull()) {
        iconLabel->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    iconLabel->setFixedSize(24, 24);
    
    // 创建文本标签
    QLabel* textLabel = new QLabel(text, button);
    textLabel->setStyleSheet("background: transparent; border: none;");
    
    layout->addWidget(iconLabel);
    layout->addWidget(textLabel);
    layout->addStretch();
    
    return button;
}

// 槽函数实现
void InformationView::onBtnInfoClicked()
{
    emit reasonSelected(ReasonType::Information, "信息");
    close();
}

void InformationView::onBtnDisturbClicked()
{
    emit reasonSelected(ReasonType::Disturb, "打扰");
    close();
}

void InformationView::onBtnRestClicked()
{
    emit reasonSelected(ReasonType::Rest, "休息");
    close();
}

void InformationView::onBtnWashroomClicked()
{
    emit reasonSelected(ReasonType::Washroom, "洗手间");
    close();
}

void InformationView::onBtnCustomClicked()
{
    customEdit->setVisible(!customEdit->isVisible());
    if (customEdit->isVisible()) {
        customEdit->setFocus();
    }
}

void InformationView::onEditCustomConfirm()
{
    QString customReason = customEdit->text().trimmed();
    if (!customReason.isEmpty()) {
        emit reasonSelected(ReasonType::Custom, customReason);
        close();
    }
}

// 鼠标事件处理
void InformationView::mouseMoveEvent(QMouseEvent *event)
{
    if (isDragging) {
        move(window_top_left_point + (event->globalPos() - mouse_startPoint));
    }
    QWidget::mouseMoveEvent(event);
}

void InformationView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = true;
        mouse_startPoint = event->globalPos();
        window_top_left_point = frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}

void InformationView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = false;
    }
    QWidget::mouseReleaseEvent(event);
}

void InformationView::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    
    // 居中显示
    if (parentWidget()) {
        QRect parentGeometry = parentWidget()->geometry();
        move(parentGeometry.center() - rect().center());
    }
}
